package net.comfycastle.musicplayer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;

/**
 * 음악 재생을 위한 액티비티
 * @author ricale
 *
 */
public class PlayActivity extends FragmentActivity implements BaseGeneralFragment.OnButtonClickListener {
	///////////
	//////////
	/////////   constant
	
	/** 현재 재생 목록 리스트 액티비티가 종료된 후 재실행될 때의 인텐트  결과를 나타내는 상수 */
	private final int ACT_SHOW_PLAYLIST = 0;
	
	///////////
	//////////
	/////////   variable
	
	/** 현재 재생 정보를 표시하는 프래그먼트 */
	private PlayInfoFragment  mInfoFragment;
	/** 현재 재생 상태를 표시하는 프래그먼트 */
	private PlayStateFragment mStateFragment;
	
	///////////
	//////////
	/////////   lifecycle callback method

	/**
	 * 액티비티가 생성되었을 때의 이벤트 리스너
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.play);
	    
	    FragmentManager fm = getSupportFragmentManager();
	    mInfoFragment  = (PlayInfoFragment)fm.findFragmentById(R.id.play_playinfo);
	    mStateFragment = (PlayStateFragment)fm.findFragmentById(R.id.play_playstate);
	    
	    Intent intent = getIntent();
	    
	    if(intent.getAction().equals(MPApp.ACT_PLAY)) {
	    	startMusicService(intent);
	    } // end if
	} // end onCreate
	
	/**
	 * 액티비티가 활성화되었을 때의 콜백 메서드
	 */
	@Override
	public void onResume() {
		super.onResume();
		MusicUtility.cancelNotification();
		if(MusicUtility.isPlaying()) {
			mInfoFragment.updateMusicInfo();
			mStateFragment.initSeekBar();
		} // end if
	} // end onResume
	
	/**
	 * 액티비티가 비활성화되었을 때의 콜백 메서드
	 */
	@Override
	public void onPause() {
		super.onPause();		
		MusicUtility.setNotification();
	} // end onPause
	
	/**
	 * 인텐트로 실행된 액티비티가 종료되며 결과를 넘겨주었을 때의 (액티비티가 재활성화되었을 때의) 콜백 메서드
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode) {
		case ACT_SHOW_PLAYLIST:
			if(resultCode == RESULT_OK) {
				if(data.getAction().equals(MPApp.ACT_PLAY)) {
			    	startMusicService(data);
			    } // end if
			} // end if
			break;
		} // end switch
	} // end onActivityResult
	
	///////////
	//////////
	/////////   methods
	
	/**
	 * 음악을 재생한다.
	 * @param intent 음악 재생을 위한 음악 목록 정보를 담은 인텐트
	 */
	private void startMusicService(Intent intent) {
		String kind = intent.getStringExtra(MPApp.ITT_GROUPKIND);
		String name = intent.getStringExtra(MPApp.ITT_GROUPNAME);
		MusicUtility.logKeyOfPlaylist(kind);
		MusicUtility.logValueOfPlaylist(name);
		
		String where, orderby;
		if(kind.equals(MPApp.TAG_ALBUM)) {
			String  artist = intent.getStringExtra(MPApp.ITT_TITLEARTIST);
			boolean isAA   = intent.getBooleanExtra(MPApp.ITT_ISALBUMARTIST, false);
			where   = MusicUtility.makeWhere(kind, name, artist, isAA);
			orderby = MPApp.TAG_TRACK;
			
			MusicUtility.logArtistOfPlaylist(artist);
			MusicUtility.logIsAAOfPlaylist(isAA);
		} else {
			where = MusicUtility.makeWhere(kind, name);
			orderby = MPApp.TAG_TITLE;
		} // end if
		
	    int index = intent.getIntExtra(MPApp.ITT_STARTTRACK, 0);
	    
		MusicListInfo mli = new MusicListInfo(where, index, orderby);
		
		Intent service = new Intent(this, MusicService.class);
		service.setAction(intent.getAction());
	    service.putExtra(MPApp.ITT_MUSICLISTINFO, mli);
	    
	    startService(service);
	} // end startMusicService
	
	///////////
	//////////
	/////////   event listener & callback method
	
	/**
	 * 버튼을 눌렀을 때의 콜백 메서드
	 * @param v 눌린 버튼
	 */
	public void onButtonClick(BaseGeneralFragment.EVENT e, View v) {
		switch(e) {
		case LIST_BUTTON_CLICK:
			Intent intent;
			intent = new Intent(PlayActivity.this, DetailActivity.class);

			if(MusicUtility.getKeyOfPlaylist().equals(MPApp.TAG_ALBUM)) {
				intent.setAction(MPApp.ACT_SHOW_PLAYLIST_INALBUM);
				intent.putExtra(MPApp.ITT_GROUPKIND,     MusicUtility.getKeyOfPlaylist());
				intent.putExtra(MPApp.ITT_GROUPNAME,     MusicUtility.getValueOfPlaylist());
				intent.putExtra(MPApp.ITT_TITLEARTIST,   MusicUtility.getArtistOfPlaylist());
				intent.putExtra(MPApp.ITT_ISALBUMARTIST, MusicUtility.getIsAAOfPlaylist());
			} else {
				intent.setAction(MPApp.ACT_SHOW_PLAYLIST_INGROUP);
				intent.putExtra(MPApp.ITT_GROUPKIND, MusicUtility.getKeyOfPlaylist());
				intent.putExtra(MPApp.ITT_GROUPNAME, MusicUtility.getValueOfPlaylist());
			} // end if

			startActivityForResult(intent, ACT_SHOW_PLAYLIST);
			break;
		} // end switch
	} // end onButtonClick
} // end PlayActivity